﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Assembler_Compiler
{
    public partial class NumberedTextBoxUC : UserControl
    {

        public NumberedTextBoxUC()
        {
            InitializeComponent();

            this.richTextBox1.Text = "Create a new file to start.";
            //this.richTextBox1.Font = new Font("Microsoft Sans Serif", 8.25f);

            //numberLabel.Font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size - 1.019f);
            numberLabel.Font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size);
        }


        private void updateNumberLabel()
        {
            //we get index of first visible char and number of first visible line
            Point pos = new Point(0, 0);
            int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
            int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);

            //now we get index of last visible char and number of last visible line
            pos.X = ClientRectangle.Width;
            pos.Y = ClientRectangle.Height;
            int lastIndex = richTextBox1.GetCharIndexFromPosition(pos) + 1;
            int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);

            //this is point position of last visible char, we'll use its Y value for calculating numberLabel size
            pos = richTextBox1.GetPositionFromCharIndex(lastIndex);


            //finally, renumber label
            numberLabel.Text = "";
            for (int i = firstLine; i <= lastLine + 1; i++)
            {
                numberLabel.Text += i + 1 + "\n";
            }

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            updateNumberLabel();

            if (this.TextChanged != null)
            {
                this.TextChanged(sender, e);
            }
        }

        private void richTextBox1_VScroll(object sender, EventArgs e)
        {
            //move location of numberLabel for amount of pixels caused by scrollbar
            int d = richTextBox1.GetPositionFromCharIndex(0).Y % (richTextBox1.Font.Height + 1);
            numberLabel.Location = new Point(0, d);

            updateNumberLabel();
        }

        private void richTextBox1_Resize(object sender, EventArgs e)
        {
            richTextBox1_VScroll(null, null);
        }

        private void richTextBox1_FontChanged(object sender, EventArgs e)
        {
            updateNumberLabel();
            richTextBox1_VScroll(null, null);
        }

        #region RichTextBox events, properties and functions

        public new event EventHandler TextChanged;

        public string[] Lines
        {
            get
            {
                return this.richTextBox1.Lines;
            }

            set
            {
                this.richTextBox1.Lines = value;
            }
        }

        public new string Text
        {
            get
            {
                return this.richTextBox1.Text;
            }

            set
            {
                this.richTextBox1.Text = value;
            }
        }

        public int SelectionStart
        {
            get
            {
                return this.richTextBox1.SelectionStart;
            }

            set
            {
                this.richTextBox1.SelectionStart = value;
            }
        }

        public int SelectionLength
        {
            get
            {
                return this.richTextBox1.SelectionLength;
            }

            set
            {
                this.richTextBox1.SelectionLength = value;
            }
        }

        public int GetFirstCharIndexFromLine(int index)
        {
            return this.richTextBox1.GetFirstCharIndexFromLine(index);
        }

        public void Select(int start, int length)
        {
            this.richTextBox1.Select(start, length);
        }

        public Font SelectionFont
        {
            get
            {
                return this.richTextBox1.SelectionFont;
            }

            set
            {
                this.richTextBox1.SelectionFont = value;
            }
        }

        public Color SelectionColor
        {
            get
            {
                return this.richTextBox1.SelectionColor;
            }

            set
            {
                this.richTextBox1.SelectionColor = value;
            }
        }

        public void ScrollToCaret()
        {
            this.richTextBox1.ScrollToCaret();
        }

        #endregion

        public void OnTextChanged()
        {
            if (this.TextChanged != null)
            {
                this.TextChanged(this.richTextBox1, null);
            }
        }
    }
}
